// NewDialog.cs
// 
// Copyright (C) 2008 Jean-André Santoni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using Gtk;
using System.IO;
using System.Collections;

namespace Astrognome
{
	public partial class NewDialog : Gtk.Dialog
	{
		private Location location;
		public MainWindow parent;
		string appdir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
		
		public NewDialog(MainWindow mainwindow)
		{
			this.Build();
			
			parent = mainwindow;
			
			countryEntry.Completion = new EntryCompletion();
			ListStore store = new ListStore(typeof(string));
			countryEntry.Completion.Model = store;
			countryEntry.Completion.TextColumn = 0;
			
			DirectoryInfo di = new DirectoryInfo(appdir + "/cities");
			FileInfo[] rgFiles = di.GetFiles("*.csv");
			foreach(FileInfo fi in rgFiles)
			{
				store.AppendValues(fi.Name.Split('.')[0]);
			}
			
			dateCalendar.Date =      DateTime.Now;
			hourSpinbutton.Value =   DateTime.Now.Hour;
			minuteSpinbutton.Value = DateTime.Now.Minute;
			secondSpinbutton.Value = DateTime.Now.Second;
		}

		protected virtual void OnCountryEntryChanged (object sender, System.EventArgs e)
		{
			string country = countryEntry.Text;
			FileInfo fi = new FileInfo(appdir + "/cities/" + country + ".csv");
			if (fi.Exists)
			{
				cityEntry.Completion = new EntryCompletion();
				ListStore store = new ListStore(typeof(string), typeof(string), typeof(string));
				cityEntry.Completion.Model = store;
				cityEntry.Completion.TextColumn = 0;
				
				CellRendererText textcell1 = new CellRendererText();
				cityEntry.Completion.PackStart(textcell1, true);
				cityEntry.Completion.AddAttribute(textcell1, "text", 1);
				
				CellRendererText textcell2 = new CellRendererText();
				cityEntry.Completion.PackStart(textcell2, true);
				cityEntry.Completion.AddAttribute(textcell2, "text", 2);
				
				StreamReader sr = new StreamReader(appdir + "/cities/" + country + ".csv");
				while( ! sr.EndOfStream)
				{
					store.AppendValues(sr.ReadLine().Split(','));
				}
				
				cityEntry.Sensitive = true;
			}
			else
			{
				cityEntry.Sensitive = false;
			}
		}

		protected virtual void OnCityEntryChanged (object sender, System.EventArgs e)
		{
			string country = countryEntry.Text;
			string city = cityEntry.Text;
			string[] tab;
			
			ArrayList cities = new ArrayList();
			
			StreamReader sr = new StreamReader(appdir + "/cities/" + country + ".csv");
			while( ! sr.EndOfStream)
			{
				tab = sr.ReadLine().Split(',');
				cities.Add(tab[0]);
				if (tab[0] == city)
				{
					location = new Location(country, tab[0], tab[1], tab[2]);
					buttonOk.Sensitive = true;
					break;
				}
			}
		}

		protected override void OnResponse(ResponseType rt)
		{
			if(rt == ResponseType.Ok)
			{
				Chart chart = new Chart();
				chart.Name = nameEntry.Text;
				chart.Location = location;
				chart.DateTime = dateCalendar.Date
					.AddHours(hourSpinbutton.Value)
					.AddMinutes(minuteSpinbutton.Value)
					.AddSeconds(secondSpinbutton.Value);
				chart.DST = dstCheckbutton.Active;
				double[] tab = new double[] {12,11,10,9.5,9,8,7,6,5,4.5,4,3.5,3,2,1,0,-1,-2,-3,-3.5,-4,-4.5,-5,-5.5,-5.75,-6,-6.5,-7,-8,-8.75,-9,-9.5,-10,-10.5,-11,-11.5,-12,-12.75,-13,-14};
				chart.TimeZone = tab[tzCombobox.Active];
				
				parent.AddPage(chart);
			}
		}
	}
}
